﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace MXComponent.Api
{
    public class Log
    {
        private static Log _instance = null;
        private static readonly object SynObject = new object();
        public static Log Instance
        {
            get
            {
                // Double-Checked Locking
                if (null == _instance)
                {
                    lock (SynObject)
                    {
                        if (null == _instance)
                        {
                            _instance = new Log();
                        }
                    }
                }
                return _instance;
            }
        }

        public void WriteLog(string content)
        {
            try
            {
                string logName = "Log_" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
                string logPath = System.Environment.CurrentDirectory + @"\Log\" + logName;
                if (!System.IO.Directory.Exists(System.Environment.CurrentDirectory + @"\Log\"))
                {
                    System.IO.Directory.CreateDirectory(System.Environment.CurrentDirectory + @"\Log\");
                }
                FileStream fs = new FileStream(logPath, FileMode.Append);
                StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("utf-8"));
                if (!File.Exists(logPath))
                {
                    File.CreateText(logPath);
                }
                sw.WriteLine("******************************************************************");
                sw.WriteLine(DateTime.Now.ToString());
                sw.Write(content);
                sw.WriteLine();
                sw.WriteLine("******************************************************************");
                sw.WriteLine();
                sw.WriteLine();
                sw.Close();
            }
            finally
            {
            }

        }
    }
}
